//
//  FabuCell.m
//  GuShiNews
//
//  Created by huachun on 2019/8/22.
//  Copyright © 2019 Jones. All rights reserved.
//

#import "FabuCell.h"
#import "AddModel.h"
#import "HiAboutTextView.h"
#import "ChooseIMGView.h"

@interface FabuCell ()<UITextViewDelegate,UITextFieldDelegate>

@property (nonatomic, strong) HiAboutTextView *textView;
@property (nonatomic, strong) UITextField     *textField;

@property (nonatomic, strong) CALayer     *Line;

@property (nonatomic, strong) UILabel         *Title_label;
@property (nonatomic, strong) ChooseIMGView     *Chooseview;

@end

@implementation FabuCell

+ (instancetype)cellForTableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"FabuCell";
    FabuCell  *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell){
        cell=[[FabuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.backgroundColor=[UIColor whiteColor];
        tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        [cell.contentView addSubview:cell.textView];
        [cell.contentView addSubview:cell.textField];
        [cell.contentView addSubview:cell.Title_label];
        [cell.contentView addSubview:cell.Chooseview];
        [cell.contentView.layer addSublayer:cell.Line];
    }
    
    return cell;
}

- (void)setModel:(AddModel *)Model{
    if (_Model != Model) {
        _Model = Model;
    }
//    if (_Model.type==1) {
//        _textField.hidden = YES;
//        _textView.hidden = NO;
//        _Line.frame=CGRectMake(25, 149.5, RECRWITCH-50, 0.9);
//        _textView.textview.placeholder = _Model.Placeholder;
//    }else if (_Model.type==0){
//        _textField.hidden = NO;
//        _textView.hidden = YES;
//        _Line.frame=CGRectMake(25, 49.5, RECRWITCH-50, 0.9);
//        _textField.placeholder = _Model.Placeholder;
//    }else{
//        _textField.hidden = NO;
//        _textView.hidden = YES;
//        _Line.frame=CGRectMake(25, 49.5, RECRWITCH-50, 0.9);
//        _textField.placeholder = _Model.Placeholder;
//    }
    
    
    _Line.hidden = NO;
    if (_Model.type==0) {
        _Chooseview.hidden = YES;
        _Title_label.hidden = YES;
        _textField.hidden = YES;
        _textView.hidden = NO;
        _Line.frame=CGRectMake(25, 149.5, RECRWITCH-50, 0.9);
        _textView.textview.placeholder = _Model.Placeholder;
    }
    else if (_Model.type==1){
        _Chooseview.hidden = YES;
        _Title_label.hidden = YES;
        _textField.hidden = NO;
        _textView.hidden = YES;
        _Line.frame=CGRectMake(25, 49.5, RECRWITCH-50, 0.9);
        _textField.placeholder = _Model.Placeholder;
    }else{
        _textField.hidden = YES;
        _textView.hidden = YES;
        _Chooseview.hidden = NO;
        _Title_label.hidden = NO;
        _Title_label.text = _Model.Placeholder;
        _Line.hidden = YES;
        _Chooseview.Array = _Model.imgarray;
    }
    _Chooseview.SuperVC = _SuperVC;
}

-(UILabel *)Title_label{
    if (!_Title_label) {
        _Title_label           = [[UILabel alloc] init];
        _Title_label.textColor = ColorMoren;
        _Title_label.frame = CGRectMake(30, 30, RECRWITCH-60, 30);
        _Title_label.font            = [UIFont boldSystemFontOfSize:18];
    }
    return _Title_label;
}

- (ChooseIMGView *)Chooseview{
    if (!_Chooseview) {
        _Chooseview         = [ChooseIMGView Show:CGRectMake(30, 60, RECRWITCH-60, (RECRWITCH-60-20)/3) Array:_Model.imgarray];
    }
    return _Chooseview;
}

- (CALayer *)Line{
    if (!_Line) {
        _Line = [CALayer new];
        _Line.frame=CGRectMake(50, 49.5, RECRWITCH-75, 0.5);
        _Line.backgroundColor=LineColor.CGColor;
    }
    return _Line;
}

- (HiAboutTextView *)textView{
    if (!_textView) {
        _textView = [HiAboutTextView show:CGRectMake(20, 0, RECRWITCH-40, 140)];
        _textView.textview.font=[UIFont boldSystemFontOfSize:17];
        _textView.textview.delegate=self;
    }
    return _textView;
}

-(void)textViewDidChange:(UITextView *)textView{
    _Model.Contont=textView.text;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField    = [[UITextField alloc] initWithFrame:CGRectMake(30, 0, RECRWITCH-60, 50)];
        _textField.textColor       = [UIColor blackColor];
        _textField.font            = [UIFont boldSystemFontOfSize:18];
        [_textField setValue:[UIFont boldSystemFontOfSize:18] forKeyPath:@"_placeholderLabel.font"];
        _textField.backgroundColor = [UIColor whiteColor];
        _textField.delegate        = self;
        _textField.layer.borderWidth = 0;
        [_textField addTarget:self action:@selector(changedTextField:) forControlEvents:UIControlEventEditingChanged];
    }
    return _textField;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (_Model.type==0) {
        if (textField.text.length >= 35) {
            return NO;
        }
    }else if (_Model.type==2){
        if (textField.text.length >= 15) {
            return NO;
        }
    }
    return YES;
}

-(void)changedTextField:(UITextField *)textField
{
    _Model.Contont=textField.text;
}

@end
